package com.gombe.credit.app.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonDialog
import com.gombe.credit.app.databinding.DialogGombeCreditUpdateBinding
import com.gombe.credit.app.databinding.DialogGombeCreditUpdateReasonItemBinding
import com.gombe.credit.app.event.GombeCreditEventPointType
import com.gombe.credit.app.utils.helper.GombeCreditConfigsHelper

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：提示dialog
 */
class DialogGombeCreditUpdate: CommonDialog {

    private lateinit var binding: DialogGombeCreditUpdateBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun takeGombeCreditRootView(): View {
        binding = DialogGombeCreditUpdateBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initGombeCreditView() {
        val releaseData = GombeCreditConfigsHelper.takeGombeCreditVersionObject()
        releaseData?.let {
            binding.textVersionName.text = "V${it.optString("versionName")}"
            //添加更新描述
            addGombeCreditUpdateReason(it.optString("versionRemark"))

            //是否强制更新
            val isMustUpdate = GombeCreditConfigsHelper.isGombeCreditUpdateMust()
            if (isMustUpdate){
                binding.buttonUpdateWait.visibility = View.GONE
                binding.imageClose.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.buttonUpdateWait.visibility = View.VISIBLE
                binding.imageClose.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun initGombeCreditListener() {
        setOnClickListener(binding.imageClose) //关闭
        setOnClickListener(binding.buttonUpdate) //立即更新
        setOnClickListener(binding.buttonUpdateWait) //稍后更新
    }

    override fun takeGombeCreditData() {
    }

    override fun onGombeCreditViewClick(v: View?) {
        when(v?.id){
            R.id.imageClose -> {
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_CLOSE,eventRemark)
            }
            R.id.buttonUpdate -> {
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_NOW,eventRemark)
                mCallback?.onUpdateClick()
            }
            R.id.buttonUpdateWait -> {
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_WITHIN,eventRemark)
            }
        }
        dismiss()
    }

    /**
     * 添加更新描述
     */
    private fun addGombeCreditUpdateReason(remark: String){
        binding.updateReasonLayout.removeAllViews()
        if (!remark.contains("$")){
            val itemBinding = DialogGombeCreditUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.textReason.text = remark
            binding.updateReasonLayout.addView(itemBinding.root)
            return
        }
        val list = remark.split("$")
        list.forEach {
            val itemBinding = DialogGombeCreditUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.textReason.text = it
            binding.updateReasonLayout.addView(itemBinding.root)
        }
    }

    interface IDialogCallback {
        //立即更新
        fun onUpdateClick()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.UPDATE_VERSION_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_DIALOG.UPDATE_VERSION_DISMISS,eventRemark)
    }
}